package it.testing.whitebox;

import static org.junit.Assert.*;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;

import it.rubrica.rubrica;
import it.testing.blackbox.ConfigurationClass;

import org.junit.After;
import org.junit.Test;

public class TestWB1 {
	
	@Test
	public void testWB1() {
		
		/*
 		 * Creazione di un file rubrica:
 		 */
 		try{
 			FileWriter file = new FileWriter
 					(ConfigurationClass.PATH_TO_FILE+"Rubrica10_testWB1");
 			
 			file.write("Giancarlo\n");
 			file.write("Capone\n");
 			file.write("081789878\n");
 			file.write("Via DIS 7\n");
 			
 			file.write("Mario\n");
 			file.write("Fiorentino\n");
 			file.write("3921800119\n");
 			file.write("Via DIS 7\n");
 			
 			file.write("Pasquale\n");
 			file.write("De Rosa\n");
 			file.write("0817778899\n");
 			file.write("Via DIS 10\n");
 			
 			file.close();
 			
 		}catch(FileNotFoundException e1){
 	 		e1.printStackTrace();
 	 		System.out.println("FileNotFoundException");
 		}catch(IOException e2){
 			e2.printStackTrace();
 			System.out.println("IOException");
 		}
 		
		BufferedReader fileBuffered = rubrica.driverLetturaFile(
					ConfigurationClass.PATH_TO_FILE+"Rubrica10_testWB1");
		
		try{
			String strLine = null;
			String letturaRubrica = "";
			while ((strLine = fileBuffered.readLine())!= null){
				letturaRubrica += strLine+"\n";
			}
			
			String risultatoAtteso= 
					"Giancarlo\n"+
				"Capone\n"+
				"081789878\n"+
				"Via DIS 7\n"+
				
				"Mario\n"+
				"Fiorentino\n"+
				"3921800119\n"+
				"Via DIS 7\n"+
				
				"Pasquale\n"+
				"De Rosa\n"+
				"0817778899\n"+
				"Via DIS 10\n";
			
			assertEquals ("Il contenuto letto dal file non coincide con il contenuto del file",
					risultatoAtteso,
					letturaRubrica);
			
		}catch(FileNotFoundException e1){
 	 		e1.printStackTrace();
 	 		System.out.println("FileNotFoundException");
 		}catch(IOException e2){
 			e2.printStackTrace();
 			System.out.println("IOException");
 		}
	}
	
	@Test
	public void testWB2(){

		ByteArrayOutputStream outContent = new ByteArrayOutputStream();	
		System.setOut(new PrintStream(outContent));
		
		rubrica.driverLetturaFile(
			ConfigurationClass.PATH_TO_FILE+"Rubrica16_testWB1");
		
		assertEquals(
				"Il file letto esiste",
				"Errore in lettura file, Nessun file trovato!\n",
				outContent.toString());

		
	}
	
	@Test
	public void testWB3() {
						
		/*
 		 * Creazione di un file rubrica:
 		 */
 		try{
 			FileWriter file = new FileWriter
 					(ConfigurationClass.PATH_TO_FILE+"Rubrica11_testWB1");
 			
 			file.write("Giancarlo\n");
 			file.write("Capone\n");
 			file.write("081789878\n");
 			file.write("Via DIS 7\n");
 			
 			file.write("Mario\n");
 			file.write("Fiorentino\n");
 			file.write("3921800119\n");
 			file.write("Via DIS 7\n");
 			
 			file.write("Pasquale\n");
 			file.write("De Rosa\n");
 			file.write("0817778899\n");
 			file.write("Via DIS 10\n");
 			
 			file.close();
 			
 		}catch(FileNotFoundException e1){
 	 		e1.printStackTrace();
 	 		System.out.println("FileNotFoundException");
 		}catch(IOException e2){
 			e2.printStackTrace();
 			System.out.println("IOException");
 		}
 		
		int[] posizioniTrovate = rubrica.driverCerca("Giancarlo", 
				ConfigurationClass.PATH_TO_FILE+"Rubrica11_testWB1").clone();
		
		assertEquals( 
				"Il contatto ricercato non coincide",
				String.valueOf(posizioniTrovate[0]),
				String.valueOf(1));
	}
	
	@Test
	public void testWB4(){

		ByteArrayOutputStream outContent = new ByteArrayOutputStream();	
		System.setOut(new PrintStream(outContent));
			
		rubrica.driverCerca("Giancarlo",
			ConfigurationClass.PATH_TO_FILE+"Rubrica15_testWB1");
		
		assertEquals(
				"Il file letto esiste",
				"Errore in lettura file..\n",
				outContent.toString());
		
	}
	
	@Test
	public void testWB5(){
		
		/*
 		 * Creazione di un file rubrica:
 		 */
 		try{
 			FileWriter file = new FileWriter
 					(ConfigurationClass.PATH_TO_FILE+"Rubrica12_testWB1");
 			
 			file.write("Giancarlo\n");
 			file.write("Capone\n");
 			file.write("081789878\n");
 			file.write("Via DIS 7\n");
 			
 			file.write("Mario\n");
 			file.write("Fiorentino\n");
 			file.write("3921800119\n");
 			file.write("Via DIS 7\n");
 			
 			file.write("Pasquale\n");
 			file.write("De Rosa\n");
 			file.write("0817778899\n");
 			file.write("Via DIS 10\n");
 			
 			file.close();
 			
 		}catch(FileNotFoundException e1){
 	 		e1.printStackTrace();
 	 		System.out.println("FileNotFoundException");
 		}catch(IOException e2){
 			e2.printStackTrace();
 			System.out.println("IOException");
 		}
 		
 		String risultatoAtteso="Nome: Giancarlo\n" +
 								"Cognome: Capone\n" +
 								"Numero Casa: 081789878\n" +
 								"Indirizzo: Via DIS 7\n\n";
 		
		String risultatoAttuale = rubrica.driverStampaRisultati(0,
						ConfigurationClass.PATH_TO_FILE+"Rubrica12_testWB1");
		
		assertEquals( 
				"Il contatto stampato non coincide",
				risultatoAtteso,
				risultatoAttuale);
		
	}
	
	@Test
	public void testWB6(){

		ByteArrayOutputStream outContent = new ByteArrayOutputStream();	
		System.setOut(new PrintStream(outContent));
			
		rubrica.driverStampaRisultati(1,
			ConfigurationClass.PATH_TO_FILE+"Rubrica14_testWB1");
		
		assertEquals(
				"Il file letto esiste",
				"Errore in lettura file..\n",
				outContent.toString());
	}
	
	@Test
	public void testWB7() throws FileNotFoundException{
		
		/*
 		 * Creazione di un file rubrica:
 		 */
 		try{
 			FileWriter file = new FileWriter
 					(ConfigurationClass.PATH_TO_FILE+"Rubrica13_testWB1");
 			
 			file.write("Giancarlo\n");
 			file.write("Capone\n");
 			file.write("081789878\n");
 			file.write("Via DIS 7\n");
 			
 			file.write("Mario\n");
 			file.write("Fiorentino\n");
 			file.write("3921800119\n");
 			file.write("Via DIS 7\n");
 			
 			file.write("Pasquale\n");
 			file.write("De Rosa\n");
 			file.write("0817778899\n");
 			file.write("Via DIS 10\n");
 			
 			file.close();
 			
 		}catch(FileNotFoundException e1){
 	 		e1.printStackTrace();
 	 		System.out.println("FileNotFoundException");
 		}catch(IOException e2){
 			e2.printStackTrace();
 			System.out.println("IOException");
 		}
 		
 		FileReader in = new FileReader(ConfigurationClass.PATH_TO_FILE+"Rubrica13_testWB1");
		BufferedReader fileBuffered = new BufferedReader(in);
		
		String risultatoAttuale = rubrica.driverStampa(fileBuffered);
 		String risultatoAtteso = "                        Nome :  Giancarlo"+
 								"                        Cognome :  Capone"+
 								"                        Indirizzo :  Via DIS 7"+
 								"                         Numero :  081789878";
 								
				
		assertEquals( 
				"Il contatto stampato non coincide",
				risultatoAtteso,
				risultatoAttuale);
		
	}
	
	@Test
	public void testWB8(){

		ByteArrayOutputStream outContent = new ByteArrayOutputStream();	
		System.setOut(new PrintStream(outContent));
			
		rubrica.driverStampa(null);
		
		assertEquals(
				"Il file letto esiste",
				"Nessun file ricevuto...\n",
				outContent.toString());
	}
	
	@After
	public void tearDown() throws Exception {

		/*
		 * Dopo aver eseguito tutta la testSuite procedo con l'eliminazione
		 * del file rubrica creato:
		 */
		
		File rubrica = new File (
				ConfigurationClass.PATH_TO_FILE+"Rubrica10_testWB1"
				);
		if (rubrica!=null
				&& rubrica.exists())
			rubrica.delete();
		
		File rubrica1 = new File (
				ConfigurationClass.PATH_TO_FILE+"Rubrica11_testWB1"
				);
		if (rubrica1!=null
				&& rubrica1.exists())
			rubrica1.delete();
		
		File rubrica2 = new File (
				ConfigurationClass.PATH_TO_FILE+"Rubrica12_testWB1"
				);
		if (rubrica2!=null
				&& rubrica2.exists())
			rubrica2.delete();
		
		File rubrica3 = new File (
				ConfigurationClass.PATH_TO_FILE+"Rubrica13_testWB1"
				);
		if (rubrica3!=null
				&& rubrica3.exists())
			rubrica3.delete();
	}

}
